package com.company.manage.system.web.controller;

import com.company.manage.common.base.BaseController;
import com.company.manage.common.persistence.Page;
import com.company.manage.common.persistence.PropertyFilter;
import com.company.manage.system.entity.Log;
import com.company.manage.system.service.LogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HuangYong on 2016/1/23.
 * desc: 日志controller
 */
@Controller
@RequestMapping("system/log")
public class LogController extends BaseController {
    private static final String TAG = "LogController";

    @Autowired
    private LogService logService;

    @RequestMapping(method = RequestMethod.GET)
    public String list(){
        return "system/logList";
    }

    /**
     * 加载日志 JSON
     * @param request
     * @return
     */
    @RequiresPermissions("sys:log:view")
    @RequestMapping("json")
    @ResponseBody
    public Map<String,Object> list(HttpServletRequest request){
        Page<Log> page = getPage(request);

        List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
        if(filters.size()>0){
            page.setPageNo(1);
        }
        page = logService.search(page,filters);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("rows",page.getResult());
        map.put("total",page.getTotalCount());
        return map;
    }

    /**
     * 根据id删除日志
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions("sys:log:delete")
    @RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String delete(@PathVariable("id") Integer id, Model model){
        logService.delete(id);
        return "success";
    }

    /**
     * 批量删除日志
     * @param logIds
     * @return
     */
    @RequiresPermissions("sys:log:delete")
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    public String delete(@RequestBody List<Integer> logIds){
        logService.deleteLog(logIds);
        return "success";
    }

    /**
     * 导出 Excel文件
     * @param request
     * @param response
     */
    @RequestMapping()
    public void exportExcel(HttpServletRequest request, HttpServletResponse response){

    }

}